﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Silverlight.Testing;
using System8ToyMC;
using System.Collections;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Automation.Peers;
using System.Windows.Automation.Provider;

namespace t_System8ToyMCControls
{
    [TestClass]
    [Exclusive]
    public class t_PlotPopUp : PresentationTest
    {
        PlotPopUp _val = new PlotPopUp();
        Button _close;

        [TestInitialize]
        public void TestSetup()
        {
            TestPanel.Children.Add(_val);
            _close = _val.FindName("CloseButton") as Button;
        }

        [TestMethod]
        public void TestCTor()
        {
            Assert.IsNotNull(_val);
            Assert.IsNotNull(_close);
        }

        [TestMethod]
        [Asynchronous]
        [Timeout(1000)]
        public void TestAddHistogram()
        {
            Histogram h = BuildBasicHistogram();
            EnqueueCallback(() =>
                {
                    TestPanel.Children.Add(h);
                    _val.SetHistogram(h);
                });
            EnqueueDelay(50);
            EnqueueCallback(() =>
            {
                Histogram foundh = FindHisto(_val);
                Assert.AreEqual(h.XAxisName, foundh.XAxisName, "The x axis is not the same");
            });
            EnqueueTestComplete();
        }

        [TestMethod]
        public void TestAddHistogramAlreadyOnThere()
        {
            Histogram h = BuildBasicHistogram();
            TestPanel.Children.Add(h);
            _val.SetHistogram(h);
        }

        [TestMethod]
        [Asynchronous]
        public void TestCloseButton()
        {
            ButtonAutomationPeer buttonPeer = new ButtonAutomationPeer(_close);
            IInvokeProvider buttonInvoker = (IInvokeProvider)buttonPeer;
            int closeCalled = 0;
            _val.ClosePopUp += () => closeCalled++;

            EnqueueCallback(() => buttonInvoker.Invoke());
            EnqueueConditional(() => closeCalled > 0);
            EnqueueCallback(() => Assert.AreEqual(1, closeCalled, "Close event fired too many times"));
            
            EnqueueTestComplete();
        }

        /// <summary>
        /// Return a histogram that is in some container
        /// </summary>
        /// <param name="_val"></param>
        /// <returns></returns>
        private Histogram FindHisto(PlotPopUp _val)
        {
            Panel g = _val.FindName("HistoContainer") as Panel;

            var histo = from i in g.Children
                        where i.GetType() == typeof(Histogram)
                        select i;

            return histo.FirstOrDefault() as Histogram;
        }

        private static Histogram BuildBasicHistogram()
        {
            Histogram h = new Histogram();
            h.XAxisName = "hi there";
            Random r = new Random();
            h.HistogramValues = (from i in Enumerable.Range(1, 100)
                                 select r.NextDouble() * 10.0).ToArray();
            return h;
        }
    }
}